<template>
  <el-dialog title="选择用户"
    :visible.sync="showFlag"
    :modal="false"
    width="80%"
    :show-close="false"
    :close-on-click-modal="false"
    :fullscreen="device === 'mobile'"
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工号" prop="employeeNo">
        <el-input v-model.trim="queryParams.employeeNo" size="small" clearable :maxlength="10" placeholder="请输入工号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model.trim="queryParams.name" clearable size="small" :maxlength="10" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input v-model.trim="queryParams.department" size="small" clearable :maxlength="100" placeholder="请输入部门"></el-input>
      </el-form-item>
      <el-form-item label="职务" prop="position">
        <el-input v-model.trim="queryParams.position" size="small" clearable :maxlength="20" placeholder="请输入职务"></el-input>
      </el-form-item>
      <el-form-item label="员工类型" prop="employeeType">
        <el-select v-model="queryParams.employeeType" size="small" clearable placeholder="请选择员工类型">
          <el-option v-for="dict in dict.type.sys_employee_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工状态" prop="employeeStatus">
        <el-select v-model="queryParams.employeeStatus" size="small" clearable placeholder="请选择员工状态">
          <el-option v-for="dict in dict.type.sys_employee_status" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="itemList" max-height="500" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" label="选择" >
        <template v-slot="scope">
          <el-radio v-model="selectedItemId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column type="index" width="50" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column property="name" label="姓名" align="center" />
      <el-table-column property="employeeNo" label="工号" align="center" />
      <el-table-column property="gender" label="性别" align="center">
        <template slot-scope="scope">
          {{ getListValue(scope.row.gender, dict.type.sys_user_sex) }}
        </template>
      </el-table-column>
      <el-table-column property="ethnicity" label="民族" align="center" />
      <el-table-column property="birthDate" label="出生日期" width="100" align="center" />
      <el-table-column property="age" label="年龄" align="center" />
      <el-table-column property="idCard" label="身份证号码" align="center" width="170" />
      <el-table-column property="politicalStatus" label="政治面貌" align="center" />
      <el-table-column property="householdType" label="户口类型" align="center" />
      <el-table-column property="phone" label="手机" width="110" align="center" />
      <el-table-column property="department" label="部门" align="center" width="240" />
      <el-table-column property="position" label="职务" align="center" />
      <el-table-column property="jobTitle" label="职位" align="center" />
      <el-table-column property="positionLevel" label="职级" align="center" />
      <el-table-column property="hireDate" label="入职日期" width="100" align="center" />
      <el-table-column property="companyYears" label="司龄(年)" align="center" />
      <el-table-column property="employeeType" label="员工类型" align="center" />
      <!-- <el-table-column property="hireDate" label="入职日期" width="100" align="center" /> -->
      <el-table-column property="employeeStatus" label="员工状态" align="center" />
      <el-table-column property="currentAddress" label="现居住地址" align="center" width="120" />
      <el-table-column property="education" label="学历" align="center" />
      <el-table-column property="graduationSchool" label="毕业院校" align="center" width="120" />
      <el-table-column property="graduationDate" label="毕业时间" width="100" align="center" />
      <el-table-column property="major" label="专业" align="center" width="120" />
      <el-table-column property="degree" label="学位" align="center" />
      <el-table-column property="emergencyContact" label="紧急联系人" align="center" width="90" />
      <el-table-column property="emergencyPhone" label="紧急联系人手机" align="center" width="120" />
      <el-table-column property="remark" label="备注" align="center" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDataListApi } from "@/api/crm/emp/roster";
import { mapGetters } from 'vuex'

export default {
  name: "RosterSelect",
  dicts: ['sys_user_sex'],
  props: {
    showFlag: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 选中数组
      selectedItemId: null,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      // 部门名称
      itemTypeName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeNo: "", // 工号
        name: "", // 姓名
        department: "", // 部门
        position: "", // 职务
        employeeType: "", // 员工类型
        employeeStatus: "", // 员工状态
      },
      // 列信息
      columns: [
        { key: 0, label: `物料/产品编码`, visible: true },
        { key: 1, label: `物料/产品名称`, visible: true },
        { key: 2, label: `规格型号`, visible: true },
        { key: 3, label: `单位`, visible: true },
        { key: 4, label: `物料/产品`, visible: true },
        { key: 5, label: `物料分类`, visible: true },
        { key: 6, label: `创建时间`, visible: true }
      ]
    };
  },
  computed: {
    ...mapGetters([
      'device'
    ]),
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
  },
  methods: {
    // 关闭弹窗
    handleCloseDialog() {
      this.$emit('update:showFlag', false);
    },
    // 获取根据list中的value返回label
    getListValue(data, list, value = 'value') {
      let label = ''
      list.map((item) => {
        if (+item[value] === +data) {
          label = item.label
        }
      })
      return label
    },
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getDataListApi(this.queryParams).then(response => {
          this.itemList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
        if(row){
            this.selectedRows = row;
        }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.handleCloseDialog()
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      // debugger;
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
        if(this.selectedItemId ==null || this.selectedItemId==0){
            this.$notify({
                title:'提示',
                type:'warning',
                message: '请至少选择一条数据!'
            });
            return;
        }
        this.$emit('onSelected',this.selectedRows);
        this.handleCloseDialog()
    }
  }
};
</script>
